import Taro, { Component } from '@tarojs/taro'
import { View, Text, Swiper, SwiperItem, Map, ScrollView } from '@tarojs/components'
import './index.scss'
import {AtSwitch, AtButton, AtInput, AtMessage, AtInputNumber, AtImagePicker, AtTabs, AtTabsPane, AtPagination, AtActivityIndicator, AtList, AtListItem, AtIcon, AtDivider, AtFloatLayout } from 'taro-ui'
import AtTabBar_index from '../../components/AtTabBar_index';
// 引入SDK核心类
import requets from '../../service/api';

var yiyuan_icon = require('../../assets/img/yiyuan_icon.png')
var bg = require('../../assets/img/bg.png')
var logo = require('../../assets/img/logo.png')
var logo2 = require('../../assets/img/logo2.png')


export default class Index extends Component {
  config = {
    navigationBarTitleText: ''
  }
  state = {
    current: 3,
    name: '',
    loading: false,
    password: '',
    valueAtSwitch:false
  }

  componentWillMount() { }

  componentDidMount() {

  }

  componentWillUnmount() { }

  componentDidShow() { }

  componentDidHide() { }
  handleChange = (v, val) => {
    this.setState({
      [v]: val
    })
  }

  onLogin = () => {

    if (!this.state.name) {
      this.handleClick('warning', '请输入用户名')
      return
    }

    if (!this.state.password) {
      this.handleClick('warning', '请输入密码')
      return
    }


    this.setState({
      loading: true
    })
    wx.login({
      success: (res) => {

        requets.baseOptions({
          url: `/zjn-web/zjn/wx/wxAppletLogin`,
          method: 'POST',
          data: { code: res.code }
        }).then((res) => {
          if (res) {
            let params = {
              loginName: this.state.name,
              password: this.state.password
            }
            requets.baseOptions({
              url: '/zjn-web//zjn/login/wxlogin',
              method: 'POST',
              data: params
            }).then((res) => {
              if (res.data.rc === 0) {

                if(res.data.ret.loginType == 1) {
                  Taro.setStorage({
                    key: "userId",
                    data: res.data.ret.userId,
                    success: () => {
  
                      Taro.navigateTo({
                        url: '/pages/index/index'
                      })
                    }
                  })
                }else{
                  Taro.setStorage({
                    key: "hospitalId",
                    data: res.data.ret.userId,
                    success: () => {
  
                      Taro.navigateTo({
                        url: '/pages/index_GL/index'
                      })
                    }
                  })
                }

                this.handleClick('success', '登录成功,即将进入...')

              } else {
                this.handleClick('warning', res.data.err)
              }
              this.setState({
                loading: false
              })
            })
          } else {
            this.handleClick('warning', res.data.err)
            this.setState({
              loading: false
            })
          }
        })
      }
    })
  }

  handleClick = (type, text) => {
    Taro.atMessage({
      'message': text,
      'type': type,
    })
  }

  handleChangeAtSwitch = value => {
    this.setState({ valueAtSwitch:value })
  }

  render() {
    const { current, loading, name, password } = this.state

    return (
      <View className='index'>
        <AtMessage />
        <Image
          className='bg'
          src={bg}
        />
        <Image
          className='logo'
          src={this.state.valueAtSwitch?logo:logo2}
        />
        <View className='shuru'>
          <AtInput
            name='value'
            title='用户名'
            type='text'
            placeholder='请输入'
            value={this.state.name}
            onChange={this.handleChange.bind(this, 'name')}
          />
          <AtInput
            name='password'
            title='密&nbsp;&nbsp;&nbsp;码'
            type='password'
            placeholder='请输入'
            value={this.state.password}
            onChange={this.handleChange.bind(this, 'password')}
          />
        </View>
        <AtSwitch size='small' border={false} className='qiehuan'  checked={this.state.valueAtSwitch} onChange={this.handleChangeAtSwitch} />
        <AtButton disabled={loading} type='primary' className='dengl' onClick={this.onLogin}>{loading ? 'loading...' : '登录'}</AtButton>
        <View  className='dengl2'>
          技术支持：苏州市伏泰信息科技股份有限公司
        </View>
      </View>
    )
  }
}
